home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / ultra250.zip / STR_DEMO.C < prev    next >
Text File  |  1992-11-12  |  20KB  |  488 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /* STR_DEMO.C                                                               */
  4. /*                                                                          */
  5. /* We have had several customers experience difficulty understanding the    */
  6. /* wn_gets function.  This is understandable, it is somewhat confusing.     */
  7. /* This demo will help you understand the features, including several new   */
  8. /* powerful features, and implement simpler forms to get basic items        */
  9. /* such as time, date, phone numbers, etc.  The modified wn_gets is fully   */
  10. /* compatible with the previous version so none of your code need change.   */
  11. /* However, a new function called wn_gets_ll (which wn_gets calls)          */
  12. /* has added the capability to uppercase the first char of each word,       */
  13. /* strip the mask completely, only strip the end, or not strip at all.      */
  14. /* It can also get input right-to-left, clear the entry on the first valid  */
  15. /* key, and return when the last character of the input is typed.           */
  16. /* Most importantly, it can allow the input of a string longer than the     */
  17. /* display width of the field and will scroll within this region auto-      */
  18. /* matically!  It can also display arrows to show the user that the string  */
  19. /* has data to the left or right of the current display field.              */
  20. /*                                                                          */
  21. /* Two newer capabilities of wn_gets_ll are the validation function hook,   */
  22. /* which can be called on each valid key pressed, and the gets hook, which  */
  23. /* is called on wn_gets_ll entry, exit, and for every key pressed.  We will */
  24. /* use the validation hook to show how you can do simple date validation,   */
  25. /* and we will use the gets hook to print out the text, mask and template   */
  26. /* strings for each key pressed, as well as make the F1 key perform a clear */
  27. /* on the input.                                                            */
  28. /*                                                                          */
  29. /* Note:  Compile this file with the warning "parameter 'ident' is never    */
  30. /*        used" turned off, as we don't have to actually use each parameter */
  31. /*        included in the validation and gets hook functions.               */
  32. /*                                                                          */
  33. /*------------------------- flags for wn_gets_ll ---------------------------*/
  34. /*                                                                          */
  35. /* G_STRIP          strips the entire mask from the string.                 */
  36. /*                * 12/04/91 = 120491, (432)-555-3421 = 4325553421                    */
  37. /*                                                                          */
  38. /* G_STRIP_END      strips the end of the string only only.                            */
  39. /*                * John Doe______ = John Doe                                                    */
  40. /*                                                                          */
  41. /* G_UP_FST_CHAR    converts the first letter of each word to uppercase.    */
  42. /*                * this is a test = This Is A Test                         */
  43. /*                                                                          */
  44. /* G_ARROW          displays left and right arrows if display width is less */
  45. /*                  than field width. NOTE: This requires an additional     */
  46. /*                  screen/window space before and after the field.         */
  47. /*                                                                          */
  48. /* G_RIGHT_TO_LEFT  gets input from the right side of the string, scooting  */
  49. /*                  characters over to the left for each valid press.       */
  50. /*                                                                          */
  51. /* G_CLEAR_ON_FIRST clears out the entry when the first valid key is        */
  52. /*                  pressed.  This saves a CTRL-Y keystroke to clear the    */
  53. /*                  entry.                                                  */
  54. /*                                                                          */
  55. /* G_EXIT_ON_FILL   causes the routine to exit whenever the last valid key  */
  56. /*                  is pressed.  This saves an extra <Enter> press.         */
  57. /*                                                                          */
  58. /*--------------------------------------------------------------------------*/
  59. /*                                                                          */
  60. /* Understanding these few flags and looking at the below examples make     */
  61. /* a very complex input routine much more managable!                        */
  62. /*                                                                          */
  63. /****************************************************************************/
  64. #include <stdlib.h>
  65. #include "uw.h"
  66. #include "uw_globx.h"
  67. #include "uw_keys.h"
  68.  
  69.  
  70. /*-------------------------- a few global variables ------------------------*/
  71. WINDOW Main_wn;
  72. char name1[32],  name2[32],  name3[32],
  73.          phone1[16], phone2[16], phone3[16],
  74.          date1[16],  date2[16],  date3[16],
  75.          time1[16],  time2[16],  time3[16];
  76.  
  77.  
  78. /*-------------------------- just some prototypes --------------------------*/
  79. void msg_line( char *msg );
  80. int my_gets(char *s, int max_len, int disp_width, int flags, int clear, WINDOW *wnp);
  81. int my_get_phone(char *s, int disp_width, int flags, WINDOW *wnp);
  82. int my_get_date(char *s, int disp_width, int flags, int clear, WINDOW *wnp);
  83. int my_get_time(char *s, int disp_width, int flags, WINDOW *wnp);
  84. int date_vld(char *w, char *t, char *m, int pos, int max_pos, WINDOW *wnp );
  85. int time_vld(char *w, char *t, char *m, int pos, int max_pos, WINDOW *wnp );
  86. int super_hook( char *text, char *template, char *mask, int pos,
  87.                                 int max_pos, int p_flag, EVENT *eventp, WINDOW *wnp );
  88.  
  89.  
  90. /*********/
  91. /* ~main */
  92. /*       ********************************************************************/
  93. /****************************************************************************/
  94. int main()
  95. {
  96.     int r;
  97.     WINDOW *wnp;
  98.  
  99.     init_video(80, 25);
  100.     init_clock(0x3333);
  101.     wnp = &Main_wn;
  102.     wn_create( 0, 0, V_cols-1, V_rows-1, DBL_BDR, WN_NORMAL, wnp );
  103.     wn_color( WHITE, BLUE, wnp );
  104.     wn_bdr_color( WHITE, BLUE, wnp );
  105.     wn_set(wnp);
  106.     wnp->inside = OFF;
  107.     wn_plst(CENTERED, 0, "<<< EnQue's String Entry Demonstration >>>", wnp);
  108.     wn_color(LIGHTGRAY, BLUE, wnp);
  109.     wn_plst(CENTERED, 1, "F1 is translated to CTRL-Y and parameters displayed by the new gets hook.", wnp);
  110.  
  111.     /* NOTE:
  112.          When we use set_gets_hook or set_validation_func, we can just call the
  113.          name of the function when using good old "C", but we have to do the
  114.          wild-looking function pointer cast when we use the C++ compiler.
  115.          If you rename this "C" file to a "CPP" file and compile under Turbo or
  116.          Borland C++ you will get a "Type mismatch in parameter func_ptr"
  117.          compiler error (not warning) if you just use the function name without
  118.          the cast!
  119.     */
  120.  
  121. #ifdef __cplusplus
  122.     set_gets_hook( (int (*)(...)) super_hook );
  123. #else
  124.     set_gets_hook( super_hook );
  125. #endif
  126.  
  127.     wn_color( WHITE, BLUE, wnp );
  128.     wnp->inside = ON;
  129.  
  130. /*---------- display full width and convert first char to uppercase --------*/
  131.     r = 2;
  132.     msg_line("Entry with first letters capped and no strip");
  133.     wn_plst(  2, r, "Enter your name  : ", wnp );
  134.     my_gets(name1, 22, 22, G_UP_FST_CHAR, 1, wnp);
  135.     wn_plst( 50, r, name1, wnp );
  136.  
  137. /*----------- display full width with strip to end and exit on fill --------*/
  138.     r++;
  139.     msg_line("Entry with strip to end and exit on fill");
  140.     wn_plst(  2, r, "Enter your name  : ", wnp );
  141.     my_gets(name2, 22, 22, G_STRIP_END | G_EXIT_ON_FILL, 1, wnp);
  142.     wn_plst( 50, r, name2, wnp );
  143.  
  144. /*---------- display only 6 characters, strip the entire mask, -------------*/
  145. /*---------- convert first char, and display arrows...         -------------*/
  146.     r++;
  147.     msg_line("Entry with first letters capped, strip, scrolling, and arrow chars");
  148.     wn_plst(  2, r, "Enter your name  :  ", wnp );
  149.     my_gets(name3, 22, 6, G_STRIP | G_UP_FST_CHAR | G_ARROW, 1, wnp);
  150.     wn_plst( 50, r, name3, wnp );
  151.  
  152. /*---------- display full width, strip the entire mask, --------------------*/
  153. /*---------- convert first char, and clear on first key --------------------*/
  154.     r++;
  155.     msg_line("Entry with first letters capped, strip, and clear on first");
  156.     wn_plst(  2, r, "Enter your name  : ", wnp );
  157. /*    my_gets(name3, 22, 22, G_STRIP | G_UP_FST_CHAR | G_CLEAR_ON_FIRST, 0, wnp);*/
  158.     my_gets(name3, 22, 22, G_STRIP | G_UP_FST_CHAR | G_INSERT | G_MOVE_TO_END, 0, wnp);
  159.     wn_plst( 50, r, name3, wnp );
  160.  
  161. /*--------- display full width, strip to end, enter right-to-left ----------*/
  162.     r += 2;
  163.     msg_line("Right-to-left entry with strip to end");
  164.     wn_plst(  2, r, "Enter your phone # : ", wnp );
  165.     my_get_phone(phone1, 14, G_STRIP_END | G_RIGHT_TO_LEFT, wnp);
  166.     wn_plst( 50, r, phone1, wnp );
  167.  
  168. /*------------------- display full width, don't strip mask -----------------*/
  169. /*------------------- and exit when the last char is entered ---------------*/
  170.     r++;
  171.     msg_line("Entry without strip and exit on fill");
  172.     wn_plst(  2, r, "Enter your phone # : ", wnp );
  173.     my_get_phone(phone2, 14, G_EXIT_ON_FILL, wnp);
  174.     wn_plst( 50, r, phone2, wnp );
  175.  
  176. /*----------------- display 5 chars and strip the entire mask --------------*/
  177.     r++;
  178.     msg_line("Entry with strip and scrolling, but without arrow chars");
  179.     wn_plst(  2, r, "Enter your phone # : ", wnp );
  180.     my_get_phone(phone3, 5, G_STRIP, wnp);
  181.     wn_plst( 50, r, phone3, wnp );
  182.  
  183. #ifdef __cplusplus
  184.     set_validation_func( (int (*)(...)) time_vld );
  185. #else
  186.     set_validation_func( date_vld );
  187. #endif
  188.  
  189. /*--------------- display full width, strip the entire mask,----------------*/
  190. /*--------------- use the validation hook to validate the date -------------*/
  191. /*--------------- and exit when the last char is entered -------------------*/
  192.     r += 2;
  193.     msg_line("Entry with strip, validation, and exit on fill");
  194.     wn_plst(  2, r, "Enter today's date: ", wnp );
  195.     my_get_date(date1, 8, G_STRIP | G_VALIDATE | G_EXIT_ON_FILL, 1, wnp);
  196.     wn_plst( 50, r, date1, wnp );
  197.  
  198. /*------------------- display full width, don't strip mask, ----------------*/
  199. /*------------------- get the input right-to-left, and ---------------------*/
  200. /*------------------- exit when the last char is entered -------------------*/
  201.     r++;
  202.     msg_line("Right-to-left entry without strip and with exit on fill");
  203.     wn_plst(  2, r, "Enter today's date: ", wnp );
  204.     my_get_date(date2, 8, G_RIGHT_TO_LEFT | G_EXIT_ON_FILL, 0, wnp);
  205.     wn_plst( 50, r, date2, wnp );
  206.  
  207. /*----------------- display 5 chars, strip the entire mask, ----------------*/
  208. /*----------------- and display with arrows --------------------------------*/
  209.     r++;
  210.     msg_line("Entry with strip, scrolling, and arrow characters");
  211.     wn_plst(  2, r, "Enter today's date:  ", wnp );
  212.     my_get_date(date3, 2, G_STRIP | G_ARROW, 1, wnp);
  213.     wn_plst( 50, r, date3, wnp );
  214.  
  215. #ifdef __cplusplus
  216.     set_validation_func( (int (*)(...)) time_vld );
  217. #else
  218.     set_validation_func( time_vld );
  219. #endif
  220.  
  221. /*--------------- display full width and strip to the end ------------------*/
  222. /*--------------- geting the input right-to-left ---------------------------*/
  223.     r += 2;
  224.     msg_line("Right-to-left entry with strip to end");
  225.     wn_plst(  2, r, "Enter current time: ", wnp );
  226.     my_get_time(time1, 5, G_STRIP_END | G_RIGHT_TO_LEFT, wnp);
  227.     wn_plst( 50, r, time1, wnp );
  228.  
  229. /*------------- display full width, don't strip mask -----------------------*/
  230. /*------------- and use the validation hook to validate the time -----------*/
  231.     r++;
  232.     msg_line("Entry without strip and with validation");
  233.     wn_plst(  2, r, "Enter current time: ", wnp );
  234.     my_get_time(time2, 5, G_VALIDATE, wnp);
  235.     wn_plst( 50, r, time2, wnp );
  236.  
  237. /*----------------- display 5 chars and strip the entire mask, -------------*/
  238. /*----------------- displaying arrows for scrolling ------------------------*/
  239.     r++;
  240.     msg_line("Entry with strip, scrolling, and arrow characters");
  241.     wn_plst(  2, r, "Enter current time:  ", wnp );
  242.     my_get_time(time3, 2, G_STRIP | G_ARROW, wnp);
  243.     wn_plst( 50, r, time3, wnp );
  244.     set_validation_func( NULL );
  245.  
  246.     set_gets_hook(NULL);
  247.  
  248.     msg_line("Demo complete, press any key to quit!");
  249.     wait_event();
  250.     wn_destroy(wnp);
  251.     end_video();
  252.     return(0);
  253. }
  254. /*** end of main ***/
  255.  
  256. /*************/
  257. /* ~msg_line */
  258. /*           ****************************************************************/
  259. /* Display the message at the last line on the screen.                      */
  260. /****************************************************************************/
  261. void msg_line( char *msg )
  262. {
  263.     push(Main_wn.att);
  264.     push(Main_wn.inside);
  265.     Main_wn.inside = OFF;
  266.     wn_color(BLACK, LIGHTGRAY, &Main_wn);
  267.     mv_cs(0, 24, &Main_wn);
  268.     wn_claol(&Main_wn);
  269.     wn_plst(CENTERED, 24, msg, &Main_wn);
  270.     pop(Main_wn.inside);
  271.     pop(Main_wn.att);
  272. }
  273. /*** end of message_line ***/
  274.  
  275. /************/
  276. /* ~my_gets */
  277. /*          *****************************************************************/
  278. /* This routine just sets up the mask and template strings and calls the    */
  279. /* wn_gets_ll function.                                                     */
  280. /****************************************************************************/
  281. int my_gets(char *s, int max_len, int disp_width, int flags, int clear, WINDOW *wnp)
  282. {
  283.     char mask[81], template[81];
  284.  
  285.     if( clear )
  286.         setmem(s,max_len,0);
  287.     setmem(mask,max_len,'_'), mask[max_len] = '\0';
  288.     setmem(template,max_len,'*'), template[max_len] = '\0';
  289.     return(wn_gets_ll(s, mask, template, swap_nibbles(wnp->att),
  290.         flags, disp_width, wnp));
  291. }
  292. /*** end of my_gets ***/
  293.  
  294. /*****************/
  295. /* ~my_get_phone */
  296. /*               ************************************************************/
  297. /* This routine just sets up the mask and template strings and calls the    */
  298. /* wn_gets_ll function.                                                     */
  299. /****************************************************************************/
  300. int my_get_phone(char *s, int disp_width, int flags, WINDOW *wnp)
  301. {
  302.     char *mask, *template;
  303.  
  304.     setmem(s,15,0);
  305.     mask     = "(___)-___-____";
  306.     template = " ###  ### ####";
  307.     return(wn_gets_ll(s, mask, template, swap_nibbles(wnp->att),
  308.         flags, disp_width, wnp));
  309. }
  310. /*** end of my_get_phone ***/
  311.  
  312. /****************/
  313. /* ~my_get_date */
  314. /*              *************************************************************/
  315. /* This routine just sets up the mask and template strings and calls the    */
  316. /* wn_gets_ll function.                                                     */
  317. /****************************************************************************/
  318. int my_get_date(char *s, int disp_width, int flags, int clear, WINDOW *wnp)
  319. {
  320.     char *mask, *template;
  321.  
  322.     if( clear )
  323.         setmem(s,9,0);
  324.     mask     = "__/__/__";
  325.     template = "## ## ##";
  326.     return(wn_gets_ll(s, mask, template, swap_nibbles(wnp->att),
  327.         flags, disp_width, wnp));
  328. }
  329. /*** end of my_get_date ***/
  330.  
  331. /****************/
  332. /* ~my_get_time */
  333. /*              *************************************************************/
  334. /* This routine just sets up the mask and template strings and calls the    */
  335. /* wn_gets_ll function.                                                     */
  336. /****************************************************************************/
  337. int my_get_time(char *s, int disp_width, int flags, WINDOW *wnp)
  338. {
  339.     char *mask, *template;
  340.  
  341.     setmem(s,6,0);
  342.     mask     = "__:__";
  343.     template = "## ##";
  344.     return(wn_gets_ll(s, mask, template, swap_nibbles(wnp->att),
  345.         flags, disp_width, wnp));
  346. }
  347. /*** end of my_get_time ***/
  348.  
  349. /*************/
  350. /* ~date_vld */
  351. /*           ****************************************************************/
  352. /* This is the validation function for dates.  The format is mm/dd/yy.      */
  353. /****************************************************************************/
  354. int date_vld(char *w, char *t, char *m, int pos, int max_pos, WINDOW *wnp )
  355. {
  356.     int ok = 1;
  357.  
  358.     if( !range('0',w[0],'1') && (w[0] != m[0]) )
  359.             ok = 0, w[0] = m[0];
  360.     if( w[0] == '0' )
  361.     {
  362.         if( !range('1',w[1],'9') && (w[1] != m[1]) )
  363.                 ok = 0, w[1] = m[1];
  364.     }
  365.     else if( w[0] == '1' )
  366.     {
  367.         if( !range('0',w[1],'2') && (w[1] != m[1]) )
  368.                 ok = 0, w[1] = m[1];
  369.     }
  370.  
  371.     if( !range('0',w[3],'3') && (w[3] != m[3]) )
  372.             ok = 0, w[3] = m[3];;
  373.     if( w[3] == '3' )
  374.     {
  375.         if( !range('0',w[4],'1') && (w[4] != m[4]) )
  376.                 ok = 0, w[4] = m[4];;
  377.     }else{
  378.         if( !range('0',w[4],'9') && (w[4] != m[4]) )
  379.                 ok = 0, w[4] = m[4];;
  380.     }
  381.     if( !range('0',w[6],'9') && (w[6] != m[6]) )
  382.             ok = 0, w[6] = m[6];;
  383.     if( !range('0',w[7],'9') && (w[7] != m[7]) )
  384.             ok = 0, w[7] = m[7];;
  385.  
  386.     if( !ok )
  387.     {
  388.         tone(1024,20);
  389.         wn_plst( CENTERED, 18, "Invalid Date - Hit any key to continue", wnp );
  390.         wait_event();
  391.         wn_claol(wnp);
  392.         return(0);
  393.     }
  394.     return(1);
  395. }
  396. /*** end of date_vld ***/
  397.  
  398. /*************/
  399. /* ~time_vld */
  400. /*           ****************************************************************/
  401. /* This is the validation function for time.  The format is hh:mm.          */
  402. /****************************************************************************/
  403. int time_vld(char *w, char *t, char *m, int pos, int max_pos, WINDOW *wnp )
  404. {
  405.     int ok = 1;
  406.  
  407.     if( !range('0',w[0],'2') && (w[0] != m[0]) )
  408.             ok = 0, w[0] = m[0];
  409.  
  410.     if( w[0] == '2' )
  411.     {
  412.         if( !range('0',w[1],'3') && (w[1] != m[1]) )
  413.                 ok = 0, w[1] = m[1];
  414.     }else{
  415.         if( !range('0',w[1],'9') && (w[1] != m[1]) )
  416.                 ok = 0, w[1] = m[1];
  417.     }
  418.     if( !range('0',w[3],'5') && (w[3] != m[3]) )
  419.             ok = 0, w[3] = m[3];;
  420.     if( !range('0',w[4],'9') && (w[4] != m[4]) )
  421.             ok = 0, w[4] = m[4];;
  422.  
  423.     if( !ok )
  424.     {
  425.         tone(1024,20);
  426.         wn_plst( CENTERED, 18, "Invalid Time - Hit any key to continue", wnp );
  427.         wait_event();
  428.         wn_claol(wnp);
  429.         return(0);
  430.     }
  431.     return(1);
  432. }
  433. /*** end of time_vld ***/
  434.  
  435. /***************/
  436. /* ~super_hook */
  437. /*             **************************************************************/
  438. /* This is the overall wn_gets_ll "super" hook.  We will use it for all     */
  439. /* entries in this demo to show the parameters on the fly!  Note that this  */
  440. /* function is only called when you call set_gets_hook(super_hook).  When   */
  441. /* you want the wn_gets_ll function quit calling this function, just use    */
  442. /* set_gets_hook(NULL).                                                     */
  443. /* Notice that the display here at the bottom of the window stays "one      */
  444. /* behind" the regular display above!  This is because this function is     */
  445. /* called from withing wn_gets_ll BEFORE the key is processed.  What this   */
  446. /* means to you is that you can take the key in the event and set it to     */
  447. /* something you like better!!!  We will take the F1 key and translate it   */
  448. /* into a CTRL-Y, commonly used to clear a string.                          */
  449. /****************************************************************************/
  450. int super_hook( char *text, char *template, char *mask, int pos,
  451.                                 int max_pos, int p_flag, EVENT *eventp, WINDOW *wnp )
  452. {
  453.     switch(p_flag)
  454.     {
  455.         case H_ENTRY:                                                                /* wn_gets_ll start                    */
  456.             break;
  457.  
  458.         case H_EXIT:                                                                /* wn_gets_ll end                        */
  459.             break;
  460.  
  461.         case H_PROCESS:                                                            /* each wn_gets_ll key!            */
  462.             if (eventp->key == KEY_F1)                                /* make F1 clear the entry!    */
  463.                 eventp->key = KEY_CTRL_Y;
  464.             break;
  465.     }
  466.     push(wnp->csr_x);                                                            /* we need to save the att  */
  467.     push(wnp->csr_y);                                                            /* and window cursor, since */
  468.     push(wnp->att);                                                                /* we are being called from */
  469.     wn_color(YELLOW, BLUE, wnp);                                    /* within wn_gets_ll!       */
  470.     mv_cs(10, 19, wnp);
  471.     wn_printf(wnp, "    Text: %-25s     Pos: %02d", text, pos);
  472.     wn_cleol(wnp);
  473.     mv_cs(10, 20, wnp);
  474.     wn_printf(wnp, "    Mask: %-25s Max Pos: %02d", mask, max_pos);
  475.     wn_cleol(wnp);
  476.     mv_cs(10, 21, wnp);
  477.     wn_printf(wnp, "Template: %-25s     Key: 0x%04X", template, eventp->key);
  478.     wn_cleol(wnp);
  479.     pop(wnp->att);
  480.     pop(wnp->csr_y);
  481.     pop(wnp->csr_x);
  482.     return(1);
  483. }
  484. /*** end of super_hook ***/
  485.  
  486.  
  487. /**** END OF FILE ****/
  488.